package eclipse.weibo.util;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;

import weibo4j.Comment;
import weibo4j.DirectMessage;
import weibo4j.Paging;
import weibo4j.Status;
import weibo4j.User;
import weibo4j.Weibo;
import weibo4j.WeiboException;
import weibo4j.org.json.JSONObject;
import weibo4j.util.DebugPrn;
import eclipse.weibo.Activator;
import eclipse.weibo.modle.WeiboView;
import eclipse.weibo.service.MyObservable;
import eclipse.weibo.service.ReadMessageThread;
import eclipse.weibo.service.ReadMyCommentThread;
import eclipse.weibo.service.ReadStatusThread;
import eclipse.weibo.service.ReadUserThread;
import eclipse.weibo.service.ShowMessageObserver;
import eclipse.weibo.service.ShowMyCommentsObserver;
import eclipse.weibo.service.ShowStatusObserver;
import eclipse.weibo.service.ShowUserGuanzhuObserver;

public class WeiboUtil {
    private static DebugPrn dp = new DebugPrn(WeiboUtil.class.getName());
    private static Weibo weibo = null;
    private static Paging weiboP = new Paging(1, 20);
    private static Paging userP = new Paging();

    static {
        //userP.setCursor(0);
        userP.setCount(20);
    }

    private static void check() {
        if (weibo != null) {
            return;
        }
        weibo = Activator.getWeibo();
    }

    public static Paging getWeiboP() {
        return weiboP;
    }

    public static Paging getUserP() {
        return userP;
    }

    public static List<Status> getFriendsTimeline() {
        List<Status> list = new ArrayList<Status>();
        check();
        try {
            list = weibo.getFriendsTimeline(weiboP);
        } catch (Exception e) {
            dp.error(e, e);
        }
        return list;
    }

    public static List<Status> getFriendsTimelineNextPage() {
        List<Status> list = new ArrayList<Status>();
        check();
        weiboP.setPage(weiboP.getPage() + 1);
        try {
            list = weibo.getFriendsTimeline(weiboP);
        } catch (Exception e) {
            dp.error(e, e);
        }
        return list;
    }

    public static List<Status> getFriendsTimelineLastPage() {
        List<Status> list = new ArrayList<Status>();
        check();
        if (weiboP.getPage() > 1) {
            weiboP.setPage(weiboP.getPage() - 1);
        } else {
            weiboP.setPage(1);
        }
        try {
            list = weibo.getFriendsTimeline(weiboP);
        } catch (Exception e) {
            dp.error(e, e);
        }
        return list;
    }

    public static List<Status> getMyWeibo(int s) {
        check();
        if (s != 0) {
            if (s == 1) {
                weiboP.setPage(weiboP.getPage() + 1);
            } else if (s == -1) {
                if (weiboP.getPage() > 1) {
                    weiboP.setPage(weiboP.getPage() - 1);
                } else {
                    weiboP.setPage(1);
                }
            }
        }
        try {
            return weibo.getUserTimeline(String.valueOf(Activator.getCurrent_user().getId()));
        } catch (Exception e) {
            dp.error(e, e);
        }

        return null;
    }

    public static List<Status> getAtMyWeiboList(int s) {
        check();
        if (s != 0) {
            if (s == 1) {
                weiboP.setPage(weiboP.getPage() + 1);
            } else if (s == -1) {
                if (weiboP.getPage() > 1) {
                    weiboP.setPage(weiboP.getPage() - 1);
                } else {
                    weiboP.setPage(1);
                }
            }
        }
        try {
            return weibo.getMentions(weiboP);
        } catch (Exception e) {
            dp.error(e, e);
        }

        return null;
    }

    public static void tt(int i) {
        try {
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            MyObservable ob = new MyObservable();
            ob.addObserver(new ShowStatusObserver(page, Display.getCurrent()));
            new ReadStatusThread(ob, i, 0).start();
            WeiboView.willCurrentView = 1;
        } catch (Exception e) {
            dp.error(e, e);
        }
    }

    public static void showUserWeibo(int i) {
        try {
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            MyObservable ob = new MyObservable();
            ob.addObserver(new ShowStatusObserver(page, Display.getCurrent()));
            ReadStatusThread t = new ReadStatusThread(ob, i, 1);
            t.start();
            WeiboView.willCurrentView = 2;
        } catch (Exception e) {
            dp.error(e, e);
        }
    }

    public static void getAtMyWeibo(int i) {
        try {
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

            MyObservable ob = new MyObservable();
            ob.addObserver(new ShowStatusObserver(page, Display.getCurrent()));
            ReadStatusThread t = new ReadStatusThread(ob, i, 2);
            t.start();
            WeiboView.willCurrentView = 3;
        } catch (Exception e) {
            dp.error(e, e);
        }
    }

    public static void showUserInfo(User u) {
        try {
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            String name = WeiboView.class.getName();
            IViewPart viewPart = page.findView(name);
            if (viewPart == null) {
                viewPart = page.showView(WeiboView.class.getName());
            }
            if (viewPart instanceof WeiboView) {
                WeiboView wv = (WeiboView) viewPart;
                wv.updateUserInfoView(u);
                page.showView(WeiboView.class.getName());
                viewPart.setFocus();
            }
        } catch (Exception e) {
            dp.error(e, e);
        }
    }

    public static void showMyComment(int i) {
        try {
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            MyObservable ob = new MyObservable();
            ob.addObserver(new ShowMyCommentsObserver(page, Display.getCurrent()));
            ReadMyCommentThread t = new ReadMyCommentThread(ob, i);
            t.start();
        } catch (Exception e) {
            dp.error(e, e);
        }
    }

    public static void showMyMessage(int i) {
        try {
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            MyObservable ob = new MyObservable();
            ob.addObserver(new ShowMessageObserver(page, Display.getCurrent()));
            ReadMessageThread t = new ReadMessageThread(ob, i);
            t.start();
        } catch (Exception e) {
            dp.error(e, e);
        }
    }

    public static void showUserGuanzhu(long userId, int i) {
        showUserGuanzhu(String.valueOf(userId), i);
    }

    public static void showUserGuanzhu(String userId, int i) {
        try {
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            MyObservable ob = new MyObservable();
            ob.addObserver(new ShowUserGuanzhuObserver(page, Display.getCurrent(), 0));
            ReadUserThread t = new ReadUserThread(ob, userId, i, 0);
            t.start();
        } catch (Exception e) {
            dp.error(e, e);
        }
    }

    public static void showUserFans(long userId, int i) {
        showUserFans(String.valueOf(userId), i);
    }

    public static void showUserFans(String userId, int i) {
        try {
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            MyObservable ob = new MyObservable();
            ob.addObserver(new ShowUserGuanzhuObserver(page, Display.getCurrent(), 1));
            ReadUserThread t = new ReadUserThread(ob, userId, i, 1);
            t.start();
        } catch (Exception e) {
            dp.error(e, e);
        }
    }

    public static void logout() {
        try {
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            IViewPart viewPart = page.findView(WeiboView.class.getName());
            if (viewPart == null) {
                dp.info(viewPart);
                viewPart = page.showView(WeiboView.class.getName());
                dp.info(viewPart);
            }
            ((WeiboView) viewPart).resetView();
            page.showView(WeiboView.class.getName());
            viewPart.setFocus();
        } catch (Exception e) {
            dp.error(e, e);
        }
    }

    public static Map<String, Pair<Integer, Integer>> getWeiboNum(String ids) {
        check();
        Map<String, Pair<Integer, Integer>> map = new HashMap<String, Pair<Integer, Integer>>();
        try {
            map = weibo.getStatusCount(ids);
        } catch (Exception e) {
            dp.error(e, e);
        }
        return map;
    }

    public static Comment addComment(String id, String comment) {
        check();
        try {
            Comment com = weibo.updateComment(comment, id, null);
            return com;
        } catch (Exception e) {
            dp.error(e, e);
        }
        return null;
    }

    public static Status reposit(String id, String status) {
        check();
        try {
            Status s = weibo.repost(id, status);
            return s;
        } catch (Exception e) {
            dp.error(e, e);
        }
        return null;
    }

    public static Status createFavorite(long id) {
        check();
        try {
            Status s = weibo.createFavorite(id);
            return s;
        } catch (Exception e) {
            dp.error(e, e);
        }
        return null;
    }

    public static void deleteStatus(long id) {
        check();
        try {
            weibo.destroyStatus(id);
        } catch (Exception e) {
            dp.error(e, e);
        }
    }

    public static void deleteComment(long id) {
        check();
        try {
            weibo.destroyComment(id);
        } catch (Exception e) {
            dp.error(e, e);
        }
    }

    public static void deleteMessage(int id) {
        check();
        try {
            weibo.destroyDirectMessage(id);
        } catch (Exception e) {
            dp.error(e, e);
        }
    }

    public static List<Comment> getWeiboComment(String id, Paging page) {
        check();
        try {
            //return weibo.getComments(id, page);
            return weibo.getComments(id);
        } catch (Exception e) {
            dp.error(e, e);
        }
        return null;
    }

    public static User getUserInfoId(String id) {
        check();
        User u = null;
        try {
            u = weibo.showUser(id);
        } catch (Exception e) {
            dp.error(e, e);
        }
        return u;
    }

    public static User getUserInfoName(String name) {
        check();
        User u = null;
        try {
            name = URLEncoder.encode(name, "UTF-8");
            u = weibo.showUserByName(name);
        } catch (Exception e) {
            dp.error(e, e);
        }
        return u;
    }

    public static boolean isFans(long id, long toId) {
        return isFans(String.valueOf(id), String.valueOf(toId));
    }

    public static boolean isFans(String id, String toId) {
        check();
        try {
            JSONObject json = weibo.showFriendships(id, toId);
            JSONObject source = json.getJSONObject("source");

            return source.getBoolean("following");
        } catch (Exception e) {
            dp.error(e, e);
        }

        return false;
    }

    public static boolean createFriend(long toId) {
        return createFriend(String.valueOf(toId));
    }

    public static boolean createFriend(String toId) {
        check();
        try {
            weibo.createFriendship(toId);
            return true;
        } catch (Exception e) {
            dp.error(e, e);
        }
        return false;
    }

    public static boolean deleteFriend(long toId) {
        return deleteFriend(String.valueOf(toId));
    }

    public static boolean deleteFriend(String toId) {
        check();
        try {
            weibo.destroyFriendship(toId);
            return true;
        } catch (Exception e) {
            dp.error(e, e);
        }
        return false;
    }

    public static List<Comment> getMyComment(int s) {
        check();
        List<Comment> list = new ArrayList<Comment>();
        if (s != 0) {
            if (s == 1) {
                weiboP.setPage(weiboP.getPage() + 1);
            } else if (s == -1) {
                if (weiboP.getPage() > 1) {
                    weiboP.setPage(weiboP.getPage() - 1);
                } else {
                    weiboP.setPage(1);
                }
            }
        }
        try {
            //list = weibo.getCommentsToMe(weiboP);
        } catch (Exception e) {
            dp.error(e, e);
        }
        return list;
    }

    public static List<DirectMessage> getMyMessage(int s) {
        check();
        List<DirectMessage> list = new ArrayList<DirectMessage>();
        if (s != 0) {
            if (s == 1) {
                weiboP.setPage(weiboP.getPage() + 1);
            } else if (s == -1) {
                if (weiboP.getPage() > 1) {
                    weiboP.setPage(weiboP.getPage() - 1);
                } else {
                    weiboP.setPage(1);
                }
            }
        }
        try {
            list = weibo.getDirectMessages(weiboP);
        } catch (Exception e) {
            dp.error(e, e);
        }
        return list;
    }

    public static List<User> getUserGuanzhu(String userId, int s) {
        check();
        if (s != 0) {
            if (s == 1) {
                //userP.setCursor(userP.getCursor() + userP.getCount());
            } else if (s == -1) {
                //if (userP.getCursor() > 0) {
                //    userP.setCursor(userP.getCursor() - userP.getCount());
                //} else {
                //    userP.setCursor(0);
                //}
            }
        }
        try {
            return weibo.getFriendsStatuses(userId, userP);
        } catch (Exception e) {
            dp.error(e, e);
        }
        return null;
    }

    public static List<User> getUserFans(String userId, int s) {
        check();
        if (s != 0) {
            if (s == 1) {
                //userP.setCursor(userP.getCursor() + userP.getCount());
            } else if (s == -1) {
                //if (userP.getCursor() > 0) {
                //    userP.setCursor(userP.getCursor() - userP.getCount());
                //} else {
                //    userP.setCursor(0);
                //}
            }
        }
        try {
            return weibo.getFollowersStatuses(userId, userP);
        } catch (WeiboException e) {
            dp.error(e, e);
        }
        return null;
    }
}